package com.igeekhome.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.igeekhome.mapper.SysRoleMapper;
import com.igeekhome.mapper.SysStaffRoleMapper;
import com.igeekhome.pojo.SysRole;
import com.igeekhome.service.ISysPermissionService;
import com.igeekhome.service.ISysRoleService;
import com.igeekhome.util.DtreeBean;
import com.igeekhome.util.ResultBean;
import com.igeekhome.util.TableResultBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 汪璇
 */
@Service("sysRoleService")
public class SysRoleServiceImpl implements ISysRoleService {
    @Autowired
    private SysRoleMapper sysRoleMapper;
    
    @Autowired
    private ISysPermissionService permissionService;
    
    @Autowired
    private SysStaffRoleMapper sysStaffRoleMapper;
    
    @Override
    public List<SysRole> selectAllRole() {
        List<SysRole> sysRoles = sysRoleMapper.selectByExample(null);
        return sysRoles;
    }
    
    
    /**
     * @Description 给页面返回查询角色的dtree树数据格式
     * @Param [list]
     * @Return com.igeekhome.util.ResultBean
     **/
    @Override
    public ResultBean roleList(List<SysRole> list) {
        List<DtreeBean> beans = new ArrayList<>();
        for (SysRole role : list) {
            DtreeBean bean = new DtreeBean();
            bean.setParentId(0);
            bean.setCheckArr("0");
            bean.setId(role.getId());
            bean.setTitle(role.getRoleName());
            beans.add(bean);
        }
        return ResultBean.ok(beans);
    }
    
    /**
     * @Description 设置员工角色
     * @Param [StaffId, roleId]
     * @Return com.igeekhome.util.ResultBean
     **/
    @Override
    public ResultBean setRole(Integer StaffId, Integer roleId) {
        try {
            
            Integer id = sysStaffRoleMapper.selectById(StaffId);
            if (id != null && "".equals(id)) {
                sysRoleMapper.updateByRoleId(StaffId, roleId);
            } else {
                sysRoleMapper.insertstaffRole(StaffId, roleId);
            }
            return ResultBean.ok();
        } catch (Exception e) {
            return ResultBean.fail("操作失败");
        }
    }
    
    /**
     * @Description 角色分配权限
     * @Param [staffIds, roleId]
     * @Return com.igeekhome.util.ResultBean
     **/
    @Transactional
    @Override
    public ResultBean disRole(Integer[] powers, Integer roleId) {
        return permissionService.disPower(powers, roleId);
    }
    
    @Override
    public TableResultBean roles(Integer page, Integer limit) {
        //开启分页
        PageHelper.startPage(page, limit);
        List<SysRole> sysRoles = sysRoleMapper.selectByExample(null);
        
        //获取总条数
        long total = new PageInfo<SysRole>(sysRoles).getTotal();
        return TableResultBean.ok(total, sysRoles);
    }
    
    @Override
    public ResultBean editRole(SysRole role) {
        return ResultBean.returnResultBran(sysRoleMapper.updateByPrimaryKeySelective(role));
    }
    
    @Override
    public ResultBean addRole(SysRole role) {
        System.out.println(role);
        SysRole r = sysRoleMapper.selectByPrimaryKey(role.getId());
        if (r == null || r.getRoleName() == null || r.getRoleName() == "") {
            return ResultBean.returnResultBran(sysRoleMapper.insert(role));
        }
        return ResultBean.fail("id已存在");
    }
    
    
    /**
     * @Description 删除角色
     * @Param [id]
     * @Return com.igeekhome.util.ResultBean
     **/
    @Transactional(rollbackFor = Exception.class)
    @Override
    public ResultBean delRole(Integer id) {
        
        // 设置回滚点
        Object savePoint = TransactionAspectSupport.currentTransactionStatus().createSavepoint();
        try {
            sysRoleMapper.updateStaffRole(id);
            sysRoleMapper.delRolePower(id);
            sysRoleMapper.deleteByPrimaryKey(id);
            return ResultBean.ok();
        } catch (Exception e) {
            e.printStackTrace();
            // 回滚到回滚点
            TransactionAspectSupport.currentTransactionStatus().rollbackToSavepoint(savePoint);
            return ResultBean.fail("数据提交失败,已回滚");
        }
    }
}
